/************************************************************************************
 *                                .                                                 *
 *                                .                                                 *
 ************************************************************************************/
package jMiiEditor.mii.introspection;

import jMiiEditor.mii.MIIGETTER;
import jMiiEditor.mii.Mii;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import cx.ath.choisnet.lang.introspection.IntrospectionInvokeException;
import cx.ath.choisnet.lang.introspection.method.AbstractIntrospectionItem;

/**
 * @author CC
 *
 */
public class MiiGetterIntrospectionItem 
    extends AbstractIntrospectionItem<Mii> 
{
    private MIIGETTER annotation = null;
    private Object minValue;
    private Object defaultValue;
    private Object maxValue;
    private MIIGETTER.Type type;
    
    public MiiGetterIntrospectionItem( final Method getter, final Method setter/*, final MIIGETTER annotation*/ )
    {
        super( getter, setter );
        
        this.annotation = super.getGetterMethod().getAnnotation( MIIGETTER.class );
        this.type       = annotation.type();
        
        if( this.annotation.minValue() >= 0 ) {
            this.minValue = new Integer( this.annotation.minValue() );
        }
        if( this.annotation.defaultValue() >= 0 ) {
            this.defaultValue = new Integer( this.annotation.defaultValue() );
        }
        if( this.annotation.maxValue() >= 0 ) {
            this.maxValue = new Integer( this.annotation.maxValue() );
        }
    }

    public byte[] getBytesValue( final Mii mii ) throws IntrospectionInvokeException
    {
        byte[] bytes = new byte[ 4 ];
        Object o     = getObjectValue( mii );

        // check o.getClass().isArray() is done by Array.getLength( o )
        final int len = Array.getLength( o );

        if( len != 4 ) {
            throw new RuntimeException( "length of return array should be 4 bytes : " + this.getGetterMethod() );
        }

        for( int i=0; i<len; i++ ) {
            bytes[ i ] = Byte.class.cast( Array.get( o, i ) );
        }

        return bytes;
    }
    
//    @Deprecated
//    public MIIGETTER getAnnotation()
//    {
//        return annotation;
//    }
    
    /**
     * @return the minValue
     */
    public Object getMinValue()
    {
        return minValue;
    }

    /**
     * @return the defaultValue
     */
    public Object getDefaultValue()
    {
        return defaultValue;
    }

    /**
     * @return the maxValue
     */
    public Object getMaxValue()
    {
        return maxValue;
    }

    /**
     * @return the type
     */
    public MIIGETTER.Type getType()
    {
        return type;
    }
    
}